<template>
	<div>
		<!-- 输入内容的地方 -->
		<editor :id="'editor' + onlyId" :style="{height: '500px'}" class="ql-container" placeholder="请输入详细内容..."
			show-img-size show-img-toolbar show-img-resize @statuschange="onStatusChange" :read-only="disabled"
			@ready="onEditorReady" @input='inputEvent'>
		</editor>



		<!-- 功能按钮 -->
		<view class='toolbar' style="border-top: 1px solid #999;" @tap="format" v-if="!disabled">
			<!-- 文字加粗 -->
			<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold">
			</view>
			
			<!-- 文字间距 -->
			<view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height"
				data-name="lineHeight" data-value="2"></view>
			
			<!-- 文字颜色 -->
			<view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color"
				data-name="color" data-value="#0000ff"></view>

			<!-- 文字背景色 -->
			<view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''" class="iconfont icon-fontbgcolor"
				data-name="backgroundColor" data-value="#00ff00">
			</view>
			
			<!-- 文字下表  -->
			<view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao"
				data-name="script" data-value="sub"></view>
			
			<!-- 文字上标 -->
			<view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao"
				data-name="script" data-value="super"></view>
			
				
			<!-- 小圆点排序 -->
			<view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie"
				data-name="list" data-value="bullet"></view>

			<!-- 图片 -->
			<view class="iconfont icon-charutupian" @tap="insertImage"></view>

			<!-- 清除所有 -->
			<view class="iconfont icon-shanchu" @tap="clear"></view>

			<!-- 分割线 -->
			<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
			
			<!-- 文字变大 -->
			<view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize"
				data-name="fontSize" data-value="24px"></view>
			

			<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont"
				style="display: flex;align-items: center;justify-content: center;" @click='isShowAllBtn = true'>
				全</view>
		</view>


		<!-- 更多按钮 -->
		<van-popup :show="isShowAllBtn" @close='isShowAllBtn = false' position="top" custom-style="height: 30%;">
			<div style="height: 100%;">
				<view class='toolbar' @tap="format" style="height: 100%;overflow-y: auto;">
					<!-- 文字倾斜 -->
					<view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic">
					</view>
					
					<!-- 下划线 -->
					<view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian"
						data-name="underline"></view>
						
					<!-- 删除线 -->
					<view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian" data-name="strike">
					</view>
				
					<!-- 有序排序 -->
					<view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie"
						data-name="list" data-value="ordered"></view>
					
					<!-- 文字靠左 -->
					<view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi" data-name="align"
						data-value="left"></view>
						
					<!-- 居中 -->
					<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
						data-name="align" data-value="center"></view>

					<!-- 右对齐 -->
					<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi"
						data-name="align" data-value="right"></view>

					<!-- 左右对其 -->
					<view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi"
						data-name="align" data-value="justify"></view>

					<!-- 文字上间距 -->
					<view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-722bianjiqi_duanqianju"
						data-name="marginTop" data-value="20px">
					</view>

					<!-- 文字下间距 -->
					<view :class="formats.marginBottom ? 'ql-active' : ''" class="iconfont icon-723bianjiqi_duanhouju"
						data-name="marginBottom" data-value="20px">
					</view>

					
					<!-- 日期 -->
					<view class="iconfont icon-date" @tap="insertDate"></view>

					<!-- 复选框 -->
					<view class="iconfont icon--checklist" data-name="list" data-value="check"></view>

					
					<!-- 撤回 -->
					<view class="iconfont icon-undo" @tap="undo"></view>

					<!-- 前进 -->
					<view class="iconfont icon-redo" @tap="redo"></view>

					<!-- 文字左间距 -->
					<view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>

					<!-- 文字右边间距 -->
					<view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>


					<!-- 文字变大 -->
					<view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
						data-name="header" :data-value="1"></view>

					
					<!-- 镜像 -->
					<view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl"
						data-name="direction" data-value="rtl"></view>
				</view>
			</div>
		</van-popup>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				readOnly: false,
				formats: {},
				content: {
					text: ""
				},
				title: "", //标题

				rowObj: {},
				editorCtx: null, //富文本编辑器上下文

				id: "",

				isShowAllBtn: false,


			}
		},

		props: {
			text: {
				required: false,
				type: String
			},
			onlyId: {
				default () {
					return 'none'
				},
				required: false,
				type: String
			},
			disabled: {
				default () {
					return false
				},
				type: Boolean,
				required: false
			}
		},

		onLoad(option) {
			// #ifndef MP-BAIDU 
			uni.loadFontFace({
				family: 'Pacifico',
				source: 'url("https://sungd.github.io/Pacifico.ttf")'
			})

			// #endif
		},

		watch: {
			text: {
				handler(newVal , oldVal) {
					console.log(newVal , '------------文本信息');
				},
				deep: true,
				immediate: true
			}
		},

		methods: {

			// 富文本内容改变
			inputEvent(e) {
				// this.value = e.detail.html 
				this.$emit('inputEventChange', e.detail)
			},

			// 富文本相关
			readOnlyChange() {
				this.readOnly = !this.readOnly
				this.isShowAllBtn = false
			},

			onEditorReady() {
				this.$nextTick(() => {
					uni.createSelectorQuery().in(this).select(`#editor${this.onlyId}`).context((
						res) => {
						console.log(res, 'res富文本编辑器');

						this.editorCtx = res.context
						if (this.text) {
							// 有值 表示编辑 复制
							this.editorCtx.setContents({
								html: this.text
							})
							this.editorCtx.scroll = false
						} else {
							// 没有值
						}
					}).exec()
				})
			},

			undo() {
				this.editorCtx.undo()
				this.isShowAllBtn = false
			},
			redo() {
				this.editorCtx.redo()
				this.isShowAllBtn = false
			},
			format(e) {
				let {
					name,
					value
				} = e.target.dataset
				if (!name) return
				this.editorCtx.format(name, value)
				this.isShowAllBtn = false
			},
			onStatusChange(e) {
				const formats = e.detail
				this.formats = formats
				this.isShowAllBtn = false
			},
			insertDivider() {
				this.editorCtx.insertDivider({
					success: function() {
						console.log('insert divider success')
					}
				})
				this.isShowAllBtn = false
			},
			clear() {
				uni.showModal({
					title: '清空编辑器',
					content: '确定清空编辑器全部内容？',
					success: res => {
						if (res.confirm) {
							this.editorCtx.clear({
								success: function(res) {
									console.log("clear success")
								}
							})
						}
					}
				})
				this.isShowAllBtn = false
			},
			removeFormat() {
				this.editorCtx.removeFormat()
				this.isShowAllBtn = false
			},
			insertDate() {
				const date = new Date()
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
				this.editorCtx.insertText({
					text: formatDate
				})
				this.isShowAllBtn = false
			},

			insertImage() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						console.log(res, '图片');
						console.log(res.tempFilePaths[res.tempFilePaths.length - 1],
							'res.tempFilePaths[res.tempFilePaths.length - 1]');
						uniCloud.uploadFile({
							filePath: res.tempFilePaths[res.tempFilePaths.length - 1],
							cloudPath: "dada.jpg",
							header: {
								"Content-Type": "multipart/form-data"
							},
						}).then(res => {
							let bas64 = res.fileID;
							this.editorCtx.insertImage({
								src: bas64,
								width: "100%",
								height: 'auto',
								extClass: "answerHtmlImage",
								alt: '图像',
							})
							this.isShowAllBtn = false

						})
					}
				})
			}
		}
	}
</script>

<style scoped>
	@import "../../static/editor-icon.css";

	.iconfont {
		cursor: pointer;
		font-size: 14px;
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: center;
		border-radius: 5px 5px 0 0;
		width: 16%;
		height: 40px;
		box-sizing: border-box;
	}

	.toolbar {
		box-sizing: border-box;
		border-bottom: 0;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
		display: flex;
		width: 100%;
		align-items: center;
		flex-wrap: wrap;
		justify-content: space-around;
		background-color: #fff;
		border-radius: 0 0 5px 5px;
	}

	.ql-container {
		box-sizing: border-box;
		padding: 0 8px;
		width: 100%;
		height: auto !important;
		background-color: #fff;
		font-size: 14px;
		line-height: 1.5;
	}

	.ql-active {
		color: #06c;
	}

	.topInput {
		width: 100%;
		background-color: #fff;
		border-radius: 5px 5px 0 0;
		border-bottom: 1px solid #e0e0e0;
	}



	.topInput span {
		font-size: 12px;
		color: #666;
		padding: 5px 8px;
		box-sizing: border-box;
		margin-bottom: 5px;
	}
</style>
